package com.eyaomall.wxmall.common.helper.sms.sms163;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.lycheeframework.core.common.util.JSONUtil;

public class Sms163Helper {

	private static final String APP_KEY = "00888107e1ec60140dded47450336b79";
	
	private static final String APP_SECRET = "331c4d3c3f7b";

	
	private static HttpClient client;
	
	static{
		client = HttpClientBuilder.create().build();
	}
	
	/**
	 * 初始化签名post方法
	 * @return
	 */
	public static HttpPost initMethod(String url){
		HttpPost method = new HttpPost(url);
		
		String nonce = String.valueOf((int) (Math.random() * 10000));
		String date = String.valueOf(Calendar.getInstance().getTime().getTime() / 1000);
		
		method.addHeader("AppKey", APP_KEY);
		method.addHeader("Nonce", String.valueOf(nonce));
		method.addHeader("CurTime", date);
		method.addHeader("CheckSum", CheckSumBuilder.getCheckSum(APP_SECRET, nonce, date));
		method.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
		
		return method;
	}

	/**
	 * 模板短信发送方法
	 * @param templateid 模板id号
	 * @param mobiles 手机号码
	 * @param params 参数的数组
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean sendSms(int templateid,String mobiles,String... params){
		HttpPost method = initMethod("https://api.netease.im/sms/sendtemplate.action");
		
		NameValuePair templateParam = new BasicNameValuePair("templateid",String.valueOf(templateid));
		
		List<String> mobilesList = new ArrayList<String>();
		mobilesList.add(mobiles);
		NameValuePair mobilesParam = new BasicNameValuePair("mobiles",JSONUtil.toString(mobilesList));
		
//		List<String> paramsList = new ArrayList<String>();
//		paramsList.add(params);
		NameValuePair paramsParam = new BasicNameValuePair("params",JSONUtil.toString(params));
		
		List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(mobilesParam);
		data.add(paramsParam);
		data.add(templateParam);
		
		HttpEntity entity;
		try {
			entity = new UrlEncodedFormEntity(data,Charset.forName("UTF-8"));
			method.setEntity(entity);
			
			HttpResponse response = client.execute(method);
			if(response.getStatusLine().getStatusCode() == 200){
				
				HttpEntity responseEntity = response.getEntity();
				String json = EntityUtils.toString(responseEntity);
				Map<String,Object> returnData = (Map<String, Object>) JSONUtil.toMap(json);
				
				if(200 == (int)returnData.get("code")){
					return true;
				}
			}else{
				return false;
			}
		} catch (Exception e) {
			//..
		}
		
		return false;
	}
	
	
	

	/**
	 * 163短信模板ID
	 * @author pro
	 *
	 */
	enum TemplateEnums{
		_LOGIN(8139),
		_REGISTER(7204),
		_FIND_PASS(8208),
		_CHANGE_PHONE(6281),
		_PLAN(6280);
		
		private int templateId;
		TemplateEnums(int templateId){
			this.templateId = templateId;
		}
		
		public int getTemplate(){
			return templateId;
		}
	}
	
	public static void main(String[] args) {
		sendSms(3033026, "13989452970","中国","123");
	}
}
